#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：图灵学习 
@File    ：6.pymysql-数据查询.py
@IDE     ：PyCharm 
@Author  ：ChenLiGao
@Date    ：2024-11-12 10:34
'''
import pymysql


def search_info():
    db = pymysql.connect(host="localhost", user="root", password="123456", port=3306, db="tuling_python")
    cursor = db.cursor()

    sql = """
            select * from employee
        """

    try:
        cursor.execute(sql)
        results = cursor.fetchall()
        print("数据查询成功：", results)
        for row in results:
            first_name = row[0]
            last_name = row[1]
            age = row[2]
            sex = row[3]
            income = row[4]
            date_time = row[5]
            print(first_name, last_name, age, sex, income, date_time)
    except Exception as e:
        print("数据查询失败：", e)
        db.rollback()  # 插入失败进行事务回滚
    finally:
        cursor.close()
        db.close()


if __name__ == '__main__':
    search_info()